package com.example.smart2.ui;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.SearchView;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.bumptech.glide.Glide;
import com.example.smart2.MainActivity;
import com.example.smart2.R;
import com.example.smart2.adapter.AppAdapter;
import com.example.smart2.adapter.PressAdapter;
import com.example.smart2.model.AppModel;
import com.example.smart2.model.BannerModel;
import com.example.smart2.model.PressCategoryModel;
import com.example.smart2.model.PressModel;
import com.example.smart2.model.ThemeModel;
import com.example.smart2.network.ApiService;
import com.google.android.material.tabs.TabLayout;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class HomeFragment extends Fragment {

    private SearchView searchView;
    private Banner banner;
    private RecyclerView rApps;
    private ImageView imageView1;
    private ImageView imageView2;
    private TextView textView1;
    private TextView textView2;
    private TabLayout tabLayout;
    private RecyclerView ryList;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view=inflater.inflate(R.layout.fragment_home, container, false);

        searchView=(SearchView) view.findViewById(R.id.searchView);
        banner=(Banner) view.findViewById(R.id.banner);
        banner.addBannerLifecycleObserver(this);
        banner.setLoopTime(3000);
        banner.setIndicator(new CircleIndicator(getActivity()));
        rApps=(RecyclerView) view.findViewById(R.id.rApps);
        rApps.setLayoutManager(new GridLayoutManager(getActivity(),5));
        imageView1=(ImageView) view.findViewById(R.id.image_hot1);
        textView1=(TextView) view.findViewById(R.id.textView_hot1);
        imageView2=(ImageView) view.findViewById(R.id.image_hot2);
        textView2=(TextView) view.findViewById(R.id.textView_hot2);
        tabLayout=(TabLayout) view.findViewById(R.id.tabLayout);
        ryList=(RecyclerView) view.findViewById(R.id.recycler);
        ryList.setLayoutManager(new LinearLayoutManager(getActivity()));


        getBanner();
        getAppList();
        getTheme();
//        getPressCategory();
//        getPressListType();
        return view;
    }

    private void getBanner(){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://124.93.196.45:10001")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        ApiService api=retrofit.create(ApiService.class);
        api.getBanner().enqueue(new Callback<BannerModel>() {
            @Override
            public void onResponse(Call<BannerModel> call, Response<BannerModel> response) {
                Log.i("tag","111");
                banner.setAdapter(new BannerImageAdapter<BannerModel.RowsBean>(response.body().getRows()) {
                    @Override
                    public void onBindView(BannerImageHolder bannerImageHolder, BannerModel.RowsBean rowsBean, int i, int i1) {
                        String str="http://124.93.196.45:10001";
                        Glide.with(getActivity())
                                .load(str+rowsBean.getAdvImg())
                                .placeholder(R.drawable.slide1)
                                .into(bannerImageHolder.imageView);
                        bannerImageHolder.imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                    }
                });
            }

            @Override
            public void onFailure(Call<BannerModel> call, Throwable throwable) {
                Log.i("tag",throwable.getMessage().toString());
            }
        });
    }

    private void getAppList(){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://124.93.196.45:10001")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        ApiService api=retrofit.create(ApiService.class);
        api.getAppList().enqueue(new Callback<AppModel>() {
            @Override
            public void onResponse(Call<AppModel> call, Response<AppModel> response) {
                AppModel appModel=response.body();
                AppModel.RowsBean more=new AppModel.RowsBean();
                more.setImgUrl("local");
                more.setServiceName("更多服务");
                more.setRemark(R.drawable.more);
                appModel.getRows().add(more);

                AppAdapter appAdapter=new AppAdapter(getActivity(),appModel,getActivity());
                rApps.setAdapter(appAdapter);
            }

            @Override
            public void onFailure(Call<AppModel> call, Throwable throwable) {

            }
        });
    }

    private void getTheme(){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://124.93.196.45:10001")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ApiService api=retrofit.create(ApiService.class);
        api.getTheme().enqueue(new Callback<ThemeModel>() {
            @Override
            public void onResponse(Call<ThemeModel> call, Response<ThemeModel> response) {
                String str="http://124.93.196.45:10001";
                ThemeModel.RowsBean rowsBean=response.body().getRows().get(0);
                Glide.with(getActivity()).load(str+rowsBean.getCover()).into(imageView1);
                textView1.setText(response.body().getRows().get(0).getTitle());
                ThemeModel.RowsBean rowsBean1=response.body().getRows().get(1);
                Glide.with(getActivity()).load(str+rowsBean1.getCover()).into(imageView2);
                textView2.setText(response.body().getRows().get(1).getTitle());
            }

            @Override
            public void onFailure(Call<ThemeModel> call, Throwable throwable) {

            }
        });
    }

    private void getPressCategory(){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://124.93.196.45:10001")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ApiService api=retrofit.create(ApiService.class);
        api.getPressCategory().enqueue(new Callback<PressCategoryModel>() {
            @Override
            public void onResponse(Call<PressCategoryModel> call, Response<PressCategoryModel> response) {
                for(int i=0;i<response.body().getData().size();i++){
                    tabLayout.addTab(tabLayout.newTab().setText(response.body().getData().get(i).getName()));
                }
            }

            @Override
            public void onFailure(Call<PressCategoryModel> call, Throwable throwable) {

            }
        });
    }

    private void getPressListType(int type){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://124.93.196.45:10001")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ApiService api=retrofit.create(ApiService.class);
        api.getPressListType(type).enqueue(new Callback<PressModel>() {
            @Override
            public void onResponse(Call<PressModel> call, Response<PressModel> response) {
                PressAdapter adapter=new PressAdapter(getActivity(),response.body());
                ryList.setAdapter(adapter);
            }

            @Override
            public void onFailure(Call<PressModel> call, Throwable throwable) {

            }
        });
    }

//    private void getPressListType(){
//        Retrofit retrofit=new Retrofit.Builder()
//                .baseUrl("http://124.93.196.45:10001")
//                .addConverterFactory(GsonConverterFactory.create())
//                .build();
//        ApiService api=retrofit.create(ApiService.class);
//        api.getPressListType().enqueue(new Callback<PressModel>() {
//            @Override
//            public void onResponse(Call<PressModel> call, Response<PressModel> response) {
//                PressAdapter adapter=new PressAdapter(getActivity(),response.body());
//                ryList.setAdapter(adapter);
//            }
//
//            @Override
//            public void onFailure(Call<PressModel> call, Throwable throwable) {
//
//            }
//        });
//    }
}